package com.syezon.wifikey.news;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.syezon.wifikey.R;
import com.syezon.wifikey.base.BaseActivity;
import com.syezon.wifikey.news.adapter.NewsTabAdapter;
import com.syezon.wifikey.news.info.YiYouLiaoNewsTabBean;
import com.syezon.wifikey.news.viewmodel.YiYouLiaoNewsTabViewModel;
import com.syezon.wifikey.utils.StatusBarUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class YiYouLiaoNewsTabActivity extends BaseActivity {
    @BindView(R.id.ll_top)
    LinearLayout mLlTop;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.vp_news)
    ViewPager mVpNews;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.app_yiyouliao_news_tab_activity;
    }

    @Override
    protected void initViewsAndEvents() {
        StatusBarUtil.setPaddingSmart(this, mLlTop);
        YiYouLiaoNewsTabViewModel newsSelfViewModel = ViewModelProviders.of(this).get(YiYouLiaoNewsTabViewModel.class);
        newsSelfViewModel.getRefreshMutableLiveData().observe(this, new Observer<List<YiYouLiaoNewsTabBean>>() {
            @Override
            public void onChanged(@Nullable List<YiYouLiaoNewsTabBean> newsTabBeans) {
                if (newsTabBeans != null) {
                    bindTabs(newsTabBeans);
                }
            }
        });
        newsSelfViewModel.getNewsTab();
    }

    private void bindTabs(final List<YiYouLiaoNewsTabBean> newsTabBeans) {
        for (YiYouLiaoNewsTabBean newsTabBean : newsTabBeans) {
            fragments.add(YiYouLiaoNewListFragment.newInstance(newsTabBean.getChannelTag()));
        }

        NewsTabAdapter newsTabAdapter = new NewsTabAdapter(getSupportFragmentManager());
        mVpNews.setAdapter(newsTabAdapter);
        newsTabAdapter.setNewsTabs(newsTabBeans);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(context);
                // load custom layout
                View customLayout = LayoutInflater.from(context).inflate(R.layout.app_news_title_tab, null);
                final TextView titleText = (TextView) customLayout.findViewById(R.id.title_text);
                titleText.setText(newsTabBeans.get(index).getChannelName());
                commonPagerTitleView.setContentView(customLayout);
                commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() {
                    @Override
                    public void onSelected(int index, int totalCount) {
                        titleText.setTextColor(Color.WHITE);
                        titleText.getPaint().setFakeBoldText(true);
                    }

                    @Override
                    public void onDeselected(int index, int totalCount) {
                        titleText.setTextColor(Color.parseColor("#66FFFFFF"));
                        titleText.getPaint().setFakeBoldText(false);
                    }

                    @Override
                    public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) {
                    }

                    @Override
                    public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) {
                    }
                });
                commonPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mVpNews.setCurrentItem(index);
                    }
                });

                return commonPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        });

        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, mVpNews);
    }



    @OnClick(R.id.ll_back)
    public void onViewClicked() {
        finish();
    }
}
